{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ee8a0fca",
   "metadata": {},
   "source": [
    "# 人脸识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "4885023f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'request_id': '1678941807,e2cdf529-0a90-4c85-9fe8-f60695504fbf',\n",
       " 'time_used': 34,\n",
       " 'faces': [{'face_token': 'a3770f8dfd498e3a1ca7056552724062',\n",
       "   'face_rectangle': {'top': 72, 'left': 85, 'width': 42, 'height': 42}}],\n",
       " 'image_id': 'vFYK5J2yxF0j5HPJhuoD7Q==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++人脸识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('D:/学习！！！/API机器学习与人工智能/img/迪丽热巴4.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\n",
    "    \"image_base64\":img,\n",
    "    'api_key':\"LQaJXA6ASeZ46Ypp5TmUzQffA31_uOfz\",\n",
    "    'api_secret':\"EzwT-yAiz3PIjkGPjkEM37fMJBZN0Sgk\"\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "6759dc4f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1678941455,50672050-581f-476f-9511-9095e3006a7f', 'time_used': 182, 'faces': [{'face_token': '58685edb309605fffe8254d6776d63be', 'face_rectangle': {'top': 72, 'left': 85, 'width': 42, 'height': 42}, 'attributes': {'gender': {'value': 'Female'}, 'age': {'value': 24}, 'smile': {'value': 6.801, 'threshold': 50.0}, 'headpose': {'pitch_angle': 6.557486, 'roll_angle': 3.310234, 'yaw_angle': -30.255306}, 'blur': {'blurness': {'value': 35.955, 'threshold': 50.0}, 'motionblur': {'value': 35.955, 'threshold': 50.0}, 'gaussianblur': {'value': 35.955, 'threshold': 50.0}}, 'emotion': {'anger': 0.144, 'disgust': 1.363, 'fear': 0.144, 'happiness': 4.82, 'neutral': 16.303, 'sadness': 18.937, 'surprise': 58.289}, 'facequality': {'value': 0.006, 'threshold': 70.1}, 'beauty': {'male_score': 75.281, 'female_score': 73.785}}}], 'image_id': 'vFYK5J2yxF0j5HPJhuoD7Q==', 'face_num': 1}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸检测\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('D:/学习！！！/API机器学习与人工智能/img/迪丽热巴4.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2. 酬载准备\n",
    "payload = {\n",
    "    'api_key':'jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov',\n",
    "    'api_secret':'Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3',\n",
    "    'image_base64':img,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,emotion,blur,beauty'\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data = payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
